import { useCallback, useEffect } from "react"

/**
 * 键盘按键-esc 事件
 * @param callback 回调函数
 */
export function useEscEvent(callback: () => void) {
  const escFunction = useCallback((event) => {
    if (event.key === "Escape") {
      callback()
    }
  }, [])

  useEffect(() => {
    document.addEventListener("keydown", escFunction, false)

    return () => {
      document.removeEventListener("keydown", escFunction, false)
    }
  }, [escFunction])
}